cc.Class({
    extends: cc.Component,

    properties: {
        menuPop: cc.Prefab,
    },

    onLoad() {
        this.menuPop = cc.instantiate(this.menuPop).getComponent('MenuPop');
        this.menuPop.node.parent = this.node.parent;
        this.node.find('btn_menu').on('click', () => {
            this.menuPop.showPop();
            cc.Proxy.newburyingPoint(EventConfig.getEventId('游戏内（菜单)'));
        }, this);
        this.node.find('btn_status').on('click', () => {
            cc.Popup.show('StatusPop');
            
        }, this);
        this.node.find('btn_open').on('click', () => this.showMenu(true), this);
    },

    onShowAward(data) {
        this.scheduleOnce(this.calculateX, 0);
        if (data == 1) {
            var award = this.node.find('btn_award');
            award.active = true;
            award.on('click', this.onAwardClick, this);

            if (!JF.Cookie.Get('FIRST_ENTER')) {
                JF.Cookie.Set('FIRST_ENTER', 'true', GLOBAL.getSecondsToAnyHour(21));
                cc.Popup.show('AwardPop', pop => this.awardPop = pop, () => this.awardPop = null);
            }
        }
    },

    onAwardClick(evt) {
        cc.Popup.show('AwardPop', pop => this.awardPop = pop, () => this.awardPop = null);
        evt.target.find('img_red').active = false;
        if (!JF.Cookie.Get('FIRST_OPEN')) {
            JF.Cookie.Set('FIRST_OPEN', 'true', GLOBAL.getSecondsToAnyHour(21));
            cc.Linker(URLS.AwardRankFlag, {
                value: LINK.gameType
            }).showLoading(false).showMessage(false)
                .request(data => data && cc.Toast.message(LangLocal.word47+'\n' + data + LangLocal.word48));
        }
    },

    updateRankFlag(cbk) {
        cc.Linker(URLS.AwardRankToday, {
            page: 1,
            pageSize: 1,
            value: LINK.gameType
        }).showLoading(false).showMessage(false)
        .request(data => {
            if (data.nowRank > 0) {
                var oldRank = JF.Cookie.Get('AWARD_PLACE');
                if (!oldRank || (oldRank && parseInt(oldRank) > data.nowRank)) cbk && cbk();
                JF.Cookie.Set('AWARD_PLACE', data.nowRank, GLOBAL.getSecondsToAnyHour(21));
            } else {
                JF.Cookie.Set('AWARD_PLACE', '');
            }
        });
    },

    updateAwardPop() {
        if (this.awardPop) this.awardPop.updatePanel();
        var award = this.node.find('btn_award');
        this.updateRankFlag(() => award.find('img_red').active = true)
    },

    calculateX() {
        this.showX = this.node.x;
        this.hideX = this.showX + (this.node.width - 70);
    },

    showMenu(show) {
        !show && this.menuPop.hidePop();
        var pos = null;
        if (show && Math.abs(this.node.x - this.hideX) < 2) {
            pos = cc.p(this.showX, this.node.y);
        } else if (!show && Math.abs(this.node.x - this.showX) < 2) {
            pos = cc.p(this.hideX, this.node.y);
        }
        pos && this.node.runAction(cc.moveTo(0.5, pos).easing(cc.easeSineInOut()));
    }

});
